from PIL import Image
from CompFunc.Hamming_Distance import DHash
from CompFunc.CuttingDetect import CDectect
import os
import sys

src = sys.argv[1]
file_dir = sys.argv[2]

src_path = os.path.join(file_dir, src)
src_img = Image.open(src_path)


class SimilarityException(Exception):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return 'The image you submit is similar to a registered image named {}'.format(self.name)


try:
    for path in os.listdir(file_dir):
        if not path == src:
            des_path = os.path.join(file_dir, path)
            des_img = Image.open(des_path)
            hamming_distance = DHash.hamming_distance(src_img, des_img)
            if hamming_distance <= 10:
                raise SimilarityException(path)
            else:

                ret = CDectect.analyse(src_path, des_path)
                if not ret[0] is None:
                    raise SimilarityException(path)
    print('The image you submit is qualified to register')

except SimilarityException as err:
    print(err)
